package com.siruier.boss.ui.activity.commom

import android.graphics.drawable.GradientDrawable
import android.view.View
import androidx.core.view.ViewCompat
import androidx.recyclerview.widget.DefaultItemAnimator
import com.siruier.boss.R
import com.siruier.boss.api.core.launchUI
import com.siruier.boss.api.core.toData
import com.siruier.boss.api.mallApi
import com.siruier.boss.bean.MobilePayPriceVo
import com.siruier.boss.common.formatPrice
import com.siruier.boss.databinding.ActivityPrepaidRefillBinding
import com.siruier.boss.databinding.ItemPrepaidRefillBinding
import com.siruier.boss.ui.api.ApiObserver
import com.siruier.boss.ui.base.BaseActivity
import com.siruier.boss.ui.base.BaseSignCheckAdapter
import com.siruier.boss.ui.ext.*
import com.siruier.boss.ui.helper.pay
import com.siruier.boss.ui.helper.payChannel
import com.siruier.boss.ui.utils.FullyGridLayoutManager
import com.yanzhenjie.sofia.Utils
import java.math.BigDecimal

class PrepaidRefillActivity : BaseActivity<ActivityPrepaidRefillBinding>() {

    private val mPrepaidRefillAdapter = PrepaidRefillAdapter()

    override fun initView() {
        Utils.invasionStatusBar(this)
        bindViewClick(vb.buttonPay, vb.titleBar.rightTextView)
        vb.titleBar.rightTextColor = R.color.white.toColor()
        vb.titleBar.rightStr = "充值记录"
    }

    override fun onClick(v: View?) {
        when (v) {
            vb.titleBar.rightTextView -> startActivityExt<PrepaidRefillRecordActivity>()
            vb.buttonPay -> {
                val phone = vb.etPhone.value
                if (phone.length != 11) {
                    "手机号码错误".toastMessageLong()
                    return
                }
                com.siruier.boss.ui.utils.Utils.hideSoftInput(this)
                launchUI({
                    // 选择支付通道
                    val mobilePayPriceVo = mPrepaidRefillAdapter.getCheck()
                    val payModel = payChannel(mobilePayPriceVo.price, BigDecimal.ZERO, false)
                    val payOrder = mallApi.mobileOrder(mobilePayPriceVo.showPrice, vb.etPhone.value).toData()
                    payModel.orderType = 4
                    payModel.orderSn = payOrder?.orderNo
                    payModel.orderPrice = payOrder?.payAmount
                    // 支付
                    val payResult = pay(payModel)
                    if (payResult) {
                        "充值成功".toastMessageLong()
                    }
                }, ApiObserver(this))
            }
        }
    }

    override fun initData() {
        launchUI({
            mPrepaidRefillAdapter.datas = mallApi.priceList().toData()?.takeIf { it.isNotEmpty() }?.also {
                it[0].check = true
            } ?: mutableListOf()
            (vb.rvList.itemAnimator as DefaultItemAnimator).supportsChangeAnimations = false
            vb.rvList.layoutManager = FullyGridLayoutManager(mThis, 3)
            vb.rvList.adapter = mPrepaidRefillAdapter
        }, ApiObserver(this))
    }

    class PrepaidRefillAdapter : BaseSignCheckAdapter<ItemPrepaidRefillBinding, MobilePayPriceVo>() {

        override fun onBindViewHolder(vb: ItemPrepaidRefillBinding, bean: MobilePayPriceVo, position: Int) {
            if (bean.check) {
                ViewCompat.setBackground(vb.rootView, GradientDrawable().apply {
                    cornerRadius = 10.dp.toFloat()
                    setColor(R.color.main_color.toColor())
                })
                vb.tvShowPrice.setTextColor(R.color.white.toColor())
                vb.tvPrice.setTextColor(R.color.white.toColor())
            } else {
                ViewCompat.setBackground(vb.rootView, GradientDrawable().apply {
                    cornerRadius = 10.dp.toFloat()
                    setStroke(1.dp, R.color.main_color.toColor())
                })
                vb.tvShowPrice.setTextColor(R.color.text_color.toColor())
                vb.tvPrice.setTextColor(R.color.main_color.toColor())
            }
            vb.tvShowPrice.text = bean.showPrice.formatPrice() + "元"
            vb.tvPrice.text = "优惠价" + bean.price.formatPrice()
            setViewClick(position, vb.rootView)
        }
    }
}